/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EliminarUsuario.java
 *
 * Created on 24/09/2011, 10:17:18 AM
 */

package GUI;

import Control.UsuarioJpaController;
import Control.exceptions.IllegalOrphanException;
import Control.exceptions.NonexistentEntityException;
import Entidad.Usuario;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Andrés 2
 */
public class EliminarUsuario extends javax.swing.JPanel {

    /** Creates new form EliminarUsuario */
    public EliminarUsuario() {
        initComponents();
        u = new Usuario();
        usuarioDao = new UsuarioJpaController();
        
        cedulaTF.addKeyListener(new KeyAdapter(){
            @Override
            public void keyTyped(KeyEvent e)    {
                char caracter = e.getKeyChar();
                if(((caracter < '0') || (caracter > '9')) &&
                (caracter != KeyEvent.VK_BACK_SPACE)){
                    e.consume();
                }
            }
        });
    }
    
    private boolean encontrado=false;
    private Usuario u;
    private UsuarioJpaController usuarioDao;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLApellido = new javax.swing.JLabel();
        jLcedula = new javax.swing.JLabel();
        jLNombre = new javax.swing.JLabel();
        buscarMonutoreadorB = new javax.swing.JButton();
        cedulaTF = new javax.swing.JTextField();
        jLFechaDeIngreso = new javax.swing.JLabel();
        label = new javax.swing.JLabel();
        nombreJL = new javax.swing.JLabel();
        correoJL = new javax.swing.JLabel();
        telefonoJL = new javax.swing.JLabel();
        cancelB = new javax.swing.JButton();
        messageJL = new javax.swing.JLabel();
        eliminarB = new javax.swing.JButton();

        jLApellido.setText("Correo");

        jLcedula.setText("Cedula");

        jLNombre.setText("Nombre");

        buscarMonutoreadorB.setText("Buscar Usuario");
        buscarMonutoreadorB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarMonutoreadorBActionPerformed(evt);
            }
        });

        cedulaTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cedulaTFActionPerformed(evt);
            }
        });

        jLFechaDeIngreso.setText("Teléfono");

        label.setText("ELIMINAR USUARIO");

        cancelB.setText("Cancelar");

        eliminarB.setText("Eliminar");
        eliminarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(eliminarB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(buscarMonutoreadorB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(messageJL, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLcedula)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cedulaTF, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLNombre)
                            .addComponent(jLApellido)
                            .addComponent(jLFechaDeIngreso))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombreJL, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(telefonoJL, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
                            .addComponent(correoJL, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLcedula)
                    .addComponent(cedulaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(buscarMonutoreadorB)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLNombre)
                    .addComponent(nombreJL, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLApellido)
                    .addComponent(correoJL, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLFechaDeIngreso)
                    .addComponent(telefonoJL, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelB)
                    .addComponent(eliminarB))
                .addGap(18, 18, 18)
                .addComponent(messageJL, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void buscarMonutoreadorBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarMonutoreadorBActionPerformed
    nombreJL.setText("");
    correoJL.setText("");
    telefonoJL.setText("");
    encontrado = false;
    u = null;
    
    if(cedulaTF.getText().equals("")){
        messageJL.setText("El campo de cedula no puede estar vacio");
    }else{
        u=usuarioDao.encontrarUsuarioPorId(Long.parseLong(cedulaTF.getText()));
    }
    
    if(u != null){
        nombreJL.setText(u.getNombre());
        if(u.getCorreoElectronico()!= null){
            correoJL.setText(u.getCorreoElectronico());
        }
        if(u.getTelefono() != null){
            telefonoJL.setText(u.getTelefono()+"");
        }
        messageJL.setText("Usuario encontrado");
        
        encontrado = true;
    }else{
        messageJL.setText("No se pudo encontrar el usuario");
        encontrado = false;
    }
}//GEN-LAST:event_buscarMonutoreadorBActionPerformed

private void eliminarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarBActionPerformed
    if(encontrado){
            try {
                try {
                    usuarioDao.destroy(Long.parseLong(cedulaTF.getText()));
                } catch (IllegalOrphanException ex) {
                    Logger.getLogger(EliminarUsuario.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(EliminarUsuario.class.getName()).log(Level.SEVERE, null, ex);
                
            }
            messageJL.setText("eliminado usuario con id"+cedulaTF.getText());
            nombreJL.setText("");
            telefonoJL.setText("");
            correoJL.setText("");
    }else{
        messageJL.setText("No hay usuarios a eliminar");
    }
}//GEN-LAST:event_eliminarBActionPerformed

private void cedulaTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cedulaTFActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cedulaTFActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarMonutoreadorB;
    private javax.swing.JButton cancelB;
    private javax.swing.JTextField cedulaTF;
    private javax.swing.JLabel correoJL;
    private javax.swing.JButton eliminarB;
    private javax.swing.JLabel jLApellido;
    private javax.swing.JLabel jLFechaDeIngreso;
    private javax.swing.JLabel jLNombre;
    private javax.swing.JLabel jLcedula;
    private javax.swing.JLabel label;
    private javax.swing.JLabel messageJL;
    private javax.swing.JLabel nombreJL;
    private javax.swing.JLabel telefonoJL;
    // End of variables declaration//GEN-END:variables

}
